/*
 * Copyright (c) 2010-2011 Josef Hardi <josef.hardi@gmail.com>
 * Copyright (c) 2010 Piotr Zagawa <piotr.zagawa@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package id.duapuluhdua.dakidaki.data.items;

import android.database.Cursor;

import id.duapuluhdua.dakidaki.data.common.DataItem;
import id.duapuluhdua.dakidaki.data.tables.DataTableGeoPoints;
import id.duapuluhdua.dakidaki.map.GeoPoint;

/**
 *
 */
public class DataItemGeoPoint extends DataItem
{
  /** Fields */
  public long trackId;

  public double wgsLon; // X - longitude
  public double wgsLat; // Y - latitude

  public int altitude;
  public int accuracy;
  public int speed;
  public int bearing;

  public long timeUtc;
  public long creationTime;

  /**
   * Default constructor.
   */
  public DataItemGeoPoint()
  {
    clear();
  }

  public DataItemGeoPoint(DataItemGeoPoint item)
  {
    super.id = item.id;

    trackId = item.trackId;

    wgsLon = item.wgsLon;
    wgsLat = item.wgsLat;

    altitude = item.altitude;
    accuracy = item.accuracy;
    speed = item.speed;
    bearing = item.bearing;

    timeUtc = item.timeUtc;
    creationTime = item.creationTime;

    mDistance = item.mDistance;
  }

  public DataItemGeoPoint(Cursor cr)
  {
    super.id = cr.getLong(DataTableGeoPoints.column.ID);

    trackId = cr.getLong(DataTableGeoPoints.column.TrackID);

    wgsLon = cr.getDouble(DataTableGeoPoints.column.Lon);
    wgsLat = cr.getDouble(DataTableGeoPoints.column.Lat);

    altitude = cr.getInt(DataTableGeoPoints.column.Altitude);
    accuracy = cr.getInt(DataTableGeoPoints.column.Accuracy);
    speed = cr.getInt(DataTableGeoPoints.column.Speed);
    bearing = cr.getInt(DataTableGeoPoints.column.Bearing);

    timeUtc = cr.getLong(DataTableGeoPoints.column.TimeUTC);
    creationTime = cr.getLong(DataTableGeoPoints.column.CreationTime);
  }

  public void clear()
  {
    trackId = -1;

    wgsLon = 0;
    wgsLat = 0;

    altitude = 0;
    accuracy = 0;
    speed = 0;
    bearing = 0;

    timeUtc = 0;
    creationTime = 0;
  }

  /**
   * Returns the distance to a geo-point in meters.
   *
   * @param wgsLon the longitude coordinate.
   * @param wgsLat the latitude coordinate.
   * @return the distance.
   */
  public double distance(double lon, double lat)
  {
    GeoPoint geoPointOrig = new GeoPoint();
    GeoPoint geoPointDest = new GeoPoint();

    geoPointOrig.wgsLon = wgsLon;
    geoPointOrig.wgsLat = wgsLat;

    geoPointDest.wgsLon = lon;
    geoPointDest.wgsLat = lat;

    return geoPointOrig.distance(geoPointDest);
  }
}
